import numpy as np
import torch


def sortrows(arr, return_sorted_indices=False):
    """
    对列进行排序，返回排序结果和索引
    :param arr:
    :param return_sorted_indices:
    :return:
    """
    arr = arr.numpy()
    key_tuple = tuple(arr[:, i] for i in range(arr.shape[1] - 1, -1, -1))
    # 使用 np.lexsort() 函数按照多列进行排序
    sorted_indices = np.lexsort(key_tuple)
    # 根据排序后的索引重新排列原始数组的行
    result = torch.tensor(arr[sorted_indices])
    return (result, torch.tensor(sorted_indices)) if return_sorted_indices else result
